package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.chenfeng.bo.CommentListBo;
import com.cskaoyan.bean.chenfeng.vo.CommentCountVo;
import com.cskaoyan.bean.chenfeng.vo.CommentListVo;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.service.CommentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author Chen_Feng
 * @since 2022/06/29 20:10
 */
@RequestMapping("wx/comment")
@RestController("wxCommentController")
public class CommentController {

    @Autowired
    CommentService commentService;

    /**
     * 获取商品评论数
     *
     * @return com.cskaoyan.bean.BaseRespVo<com.cskaoyan.bean.chenfeng.vo.CommentCountVo>
     * @author Chen_Feng
     * @since 2022/06/29 20:15
     */
    @RequestMapping("count")
    public BaseRespVo<CommentCountVo> count(Integer valueId, Integer type) {

        // 执行业务层操作
        try {
            CommentCountVo data = commentService.getCommentCount(valueId, type);
            return BaseRespVo.ok(data);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("数据异常");
        }
    }

    /**
     * 获取指定商品的所有信息
     *
     * @param commentListBo 接受到的信息封装为对象
     * @param bindingResult 用于检测是否接收到合法数据
     * @return com.cskaoyan.bean.BaseRespVo<com.cskaoyan.bean.chenfeng.vo.CommentListVo>
     * @author Chen_Feng
     * @since 2022/06/29 20:42
     */
    @RequestMapping("list")
    public BaseRespVo<CommentListVo> list(@Valid CommentListBo commentListBo, BindingResult bindingResult) {

        // 参数校验
        if (bindingResult.hasFieldErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            String field = fieldError.getField();//请求参数名
            Object rejectedValue = fieldError.getRejectedValue();//错误的值
            String defaultMessage = fieldError.getDefaultMessage(); //错误的消息
            String msg = "请求参数" + field + "没有通过校验：值为" + rejectedValue + ";" + defaultMessage;
            return BaseRespVo.invalidParameter(msg);
        }

        // 执行Service层
        try {
            CommonData<CommentListVo> data = commentService.getCommentListWithPage(commentListBo);
            return BaseRespVo.ok(data);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("获取信息失败");
        }
    }

    /**
     * 发送评论
     *
     * @param marketComment 封装了评论信息的对象
     * @return com.cskaoyan.bean.BaseRespVo<com.cskaoyan.bean.MarketComment>
     * @author Chen_Feng
     * @since 2022/06/29 22:13
     */
    @RequestMapping("post")
    public BaseRespVo<MarketComment> post(@RequestBody MarketComment marketComment) {

        // 执行Service层
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return BaseRespVo.invalidParameter("还没有登录哦");
        }
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        marketComment.setUserId(user.getId());
        try {
            MarketComment data = commentService.PostComment(marketComment);
            return BaseRespVo.ok(data);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData("评论失败");
        }
    }

}
